package com.xiaoyg.lock;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义一个共享变量number
 * 对number进行1000次的++操作
 * 使用5个线程操作
 */
public class TestAtomicitySync {

    private static int number = 0;

    private static Object obj = new Object();

    public static void main(String[] args) {
        Runnable runnable = ()->{
            for (int i = 0; i < 1000; i++) {
                /**
                 *   14: monitorenter
                 *   15: getstatic     #14                 // Field number:I
                 *   18: iconst_1
                 *   19: iadd
                 *   20: putstatic     #14                 // Field number:I
                 *   23: aload_1
                 *   24: monitorexit
                 */
                synchronized (obj){
                    number++;
                }
            }
        };

        List<Thread> list = new ArrayList<>();

        for (int i = 0; i < 5; i++) {
            Thread thread = new Thread(runnable);
            thread.start();
            list.add(thread);
        }
        System.out.printf("number: "+number);
    }

}
